﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DevExpress.Xpf.Grid;
using System.Text.RegularExpressions;
using GuiLabs.Undo;

namespace Geometry2D.Tracker
{
    public class trackerGrid:DataGrid
    {
        public PilotWindow followPilot { get; set; }
        public TrackList trackerList { get; set; }

        public int countTracker;

        public trackerGrid(PilotWindow pilot)
        {
            this.Columns.Clear();
            this.Background = Brushes.White;
            trackerList = new TrackList();
            this.AutoGenerateColumns = true;
            this.ItemsSource = trackerList.List;
            this.IsReadOnly = true;
            this.AutoGenerateColumns=true;
            this.followPilot = pilot;
            this.countTracker = 0;

            this.trackerList.mListChanged += new EventHandler(trackerList_mListChanged);
            this.followPilot.DrawingHost.DrawingControl.Drawing.ActionManager.CollectionChanged += new EventHandler(ActionManager_CollectionChanged);
        }

        void ActionManager_CollectionChanged(object sender, EventArgs e)
        {
            string actionType = followPilot.DrawingHost.DrawingControl.Drawing.ActionManager.actionType;
            IAction action = followPilot.DrawingHost.DrawingControl.Drawing.ActionManager.CurrentAction;

            string figueActionType = action.GetType().ToString();
            figueActionType = figueActionType.Replace("DynamicGeometry.", string.Empty);

            string figueType = string.Empty;
            string figueName = string.Empty;
            string effect = string.Empty;

            if (action.GetType() == typeof(DynamicGeometry.AddFigureAction))
            {
                DynamicGeometry.AddFigureAction addAction = action as DynamicGeometry.AddFigureAction;
                figueName = addAction.Figure.Name;
                figueType = addAction.Figure.GetType().ToString();
                figueType = figueType.Replace("DynamicGeometry.", string.Empty);
            }
            else if (action.GetType() == typeof(DynamicGeometry.AddItemAction<>))
            {
                DynamicGeometry.AddItemAction<Action> addItemAction = action as DynamicGeometry.AddItemAction<Action>;
            }
            else if (action.GetType() == typeof(DynamicGeometry.GeometryAction))
            {
                DynamicGeometry.GeometryAction geometryAction = action as DynamicGeometry.GeometryAction;
            }
            else if (action.GetType() == typeof(DynamicGeometry.MoveAction))
            {
                DynamicGeometry.MoveAction moveAction = action as DynamicGeometry.MoveAction;
                List<DynamicGeometry.IMovable> listPoint = moveAction.Points as List<DynamicGeometry.IMovable>;
                DynamicGeometry.IMovable startPoint = listPoint[0];
                DynamicGeometry.IMovable endPoint = listPoint[listPoint.Count - 1];
                List<DynamicGeometry.IFigure> fList = moveAction.ToRecalculate as List<DynamicGeometry.IFigure>;
                foreach(var obj in fList)
                {
                    figueName = figueName + obj.Name;
                }
                figueName.Remove(0,1);
                effect = string.Format("[{0},{1}]->[{2},{3}]",
                    Math.Round(startPoint.Coordinates.X, 2), Math.Round(startPoint.Coordinates.Y, 2),
                    Math.Round(endPoint.Coordinates.X, 2), Math.Round(endPoint.Coordinates.Y, 2));
            }
            else if (action.GetType() == typeof(DynamicGeometry.PasteAction))
            {
                DynamicGeometry.PasteAction pasteAction = action as DynamicGeometry.PasteAction;

            }
            else if (action.GetType() == typeof(DynamicGeometry.RemoveFigureAction))
            {
                DynamicGeometry.RemoveFigureAction removeFigureAction = action as DynamicGeometry.RemoveFigureAction;
                figueName = removeFigureAction.Figure.Name;
                figueType = removeFigureAction.Figure.GetType().ToString();
                figueType = figueType.Replace("DynamicGeometry.", string.Empty);
            }
            else if (action.GetType() == typeof(DynamicGeometry.RemoveFiguresAction))
            {
                DynamicGeometry.RemoveFiguresAction removeFiguresAction = action as DynamicGeometry.RemoveFiguresAction;
                foreach (var figure in removeFiguresAction.Figures)
                {
                    string name = figure.Name;
                    string type = figure.GetType().ToString();
                    type = type.Replace("DynamicGeometry.", string.Empty);
                    string context = string.Format("- {0} {1}", type, name);
                    figueType = figueType + context;
                }
                figueType.Remove(0, 1);
            }
            else if (action.GetType() == typeof(DynamicGeometry.RemoveItemAction<>))
            {
                DynamicGeometry.RemoveItemAction<Action> removeItemAction = action as DynamicGeometry.RemoveItemAction<Action>;
            }
            else if (action.GetType() == typeof(DynamicGeometry.ReplaceFigureAction))
            {
                DynamicGeometry.ReplaceFigureAction replace = action as DynamicGeometry.ReplaceFigureAction;
            }
            else if (action.GetType() == typeof(DynamicGeometry.SetPropertyAction))
            {
                DynamicGeometry.SetPropertyAction setPropertyAction = action as DynamicGeometry.SetPropertyAction;
                object newValue = setPropertyAction.NewValue;
                string typeProperty = setPropertyAction.Property.Name;
                var o = setPropertyAction.Property.Parent;
                DynamicGeometry.IFigure figure = o as DynamicGeometry.IFigure;
                figueName = figure.Name;
                figueType = figure.GetType().ToString();
                figueType = figueType.Replace("DynamicGeometry.", string.Empty);
                effect = string.Format("{0}:{1}", typeProperty, newValue);
            }

            countTracker++;
            if (countTracker % 10 == 0)
                trackerList.List.Clear();

            this.Add(new Tracker.trackRecord { Number = countTracker, ActionType = actionType, FigureActionType = figueActionType, FigureType = figueType, FigureName = figueName, Effect = effect });
        }

        public void Add(trackRecord record)
        {
            this.trackerList.Add(record);
            this.trackerChannged(this, new EventArgs());
        }

        public void Track(IAction action)
        {
            
        }

        public event EventHandler trackerChannged;

        void trackerList_mListChanged(object sender, EventArgs e)
        {
            this.Items.Refresh();
        }
    }
}
